package keyboaUtil.userData;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 2B键盘
 * @version 1.0
 * @create: 2018-04-20 8:42
 * @desc session储存
 **/
public class MySession {

    /**
     * 给一个空的httpsession
     */
    private HttpSession session = null;
    /**
     * 创建一个构造器负责将session实例化
     */
    public MySession(HttpServletRequest request){
        session = request.getSession();
    }

    /**
     * 给session赋值
     * @param request
     * @param key
     * @param val
     */
    public void set(String key,Object val) {
        this.session.setAttribute(key,val);
    }

    /**
     * 清空session
     * @param request
     */
    public void empty(){
        this.session.invalidate();
    }

    /**
     * 通过name获取session
     * @param name
     * @return
     */
    public Object getSessionByName(String key){
        if(session.getAttribute(key) != null){
            return session.getAttribute(key);
        }
        return null;
    }

}
